guard
expression is used for catching and handling exceptions. It provides an extra layer of exception handling while ScriptX executes the guarded expression, which is often a compound expression. If this expression is a compound expression, other guard
expressions can be nested dynamically inside it.
The guard
expression has two optional clauses. The catching
clause is executed only if an exception occurs. It supplies a list of exception tags, known as catchers, that are paired with expressions. These catchers can be exception classes, or they can be instances of those classes.
guardExpr ::= guard expr [ catching catchList ] [ on exit expr ] end
The on
exit
clause allows a script to supply an expression that will be executed automatically when the flow of control leaves the guard
expression. ScriptX is guaranteed to execute the expression in the on
exit
clause, whether or not an exception is reported.
The catching
clause resembles the case
expression, but it actually functions quite differently. Each entry in the catch list is called a catcher. When an exception occurs, ScriptX matches the exception with every catcher on the catch list. After each entry in the catch list has been tested, ScriptX executes the expression in the on
exit
clause, if one is present, and throws the exception again.
catchList ::= catcher moreCatchers
moreCatchers ::= [ endOfLine ]* catchList
| [ endOfLine ]* moreCatchers
| empty
catcher ::= symbol [ symbol ] [ : expr ]
The following constructs apply to the catching
clause. The reserved word all
, used as a symbol in the catch list, catches all exceptions. Note that all
is not analogous with otherwise
in the case
expression. Evaluation of the script resumes with the next item in the catch list after the expression associated with all
has been executed. An all
catcher can be at the beginning, at the end, or anywhere in the middle of a catch list, and there can even be multiple all
catchers in a catch list.
The reserved word caught
acts as a function, and catches the exception. It serves as a break point, causing the flow of control to leave the catch list. caught
requires an expression, which is evaluated and serves as the return value of the guard
expression. If the expression is not caught, evaluation continues until the end of the catch list is reached.
The reserved word throw
again
, defined as a block control expression, causes the flow of control to leave the current catch list, but it reports the exception again. If the guard
expression is called within another guard
expression, evaluation switches to the catching
clause in the next surrounding guard
expression.
This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.